package com.example.leetdemo.finash;

public class Solution419 {
    public int countBattleships(char[][] board) {
        int count = 0;
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                if (board[i][j] == 'X') {
                    if (i != board.length - 1 && board[i + 1][j] == 'X') {
                        for (int k = i; k < board.length; k++) {
                            if (board[k][j] == 'X') {
                                board[k][j] = '.';
                            } else {
                                break;
                            }
                        }
                    } else {
                        for (int k = j; k < board[i].length; k++) {
                            if (board[i][k] == 'X') {
                                board[i][k] = '.';
                            } else {
                                j = k;
                                break;
                            }
                        }
                    }
                    count++;
                }
            }
        }
        return count;
    }
}
